/* 
 * File:   tpcd.cc
 * Author: yxl072100
 *
 * Created on January 29, 2009, 2:52 PM
 */

#include <stdlib.h>

#include <stdio.h>

#include "lapackUtil.h"
#include "comp_llpmatrix.h"
#include "lpmatrix.h"
#include "pcdwrapper.h"

#include <string>
#include <fstream>
#include <iostream>

#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>




double* textendpcd(std::vector<unsigned int> I, std::vector<unsigned int> J, std::vector<double> S, int n) {
    double* result = NULL;
    int i, length=n*n;

    result = new double[length];
    if(NULL==result) {
        printf("textendpcd: Allocation fail\n");
        return NULL;
    }

    for(i=0; i<length; ++i) {
        result[i] = 0;
    }

    unsigned int row, col;
    double val;
    int piv;

    i=0;

    BOOST_FOREACH(row, I) {
        col = J[i];
        val = S[i];

        ++i;

        piv = (row-1)*n+col-1;
        result[piv] = val;
    }

    for(i=0; i<n; ++i) {
        for(int j=0; j<i; ++j) {
            val = 0;
            piv = i*n + j;
            val += result[piv];
            piv = j*n + i;
            val += result[piv];
            val = val/2;

            piv = i*n + j;
            result[piv] = val;
            piv = j*n + i;
            result[piv] = val;
        }
    }

    return result;
}


/*
 * 
 */
int main(int argc, char** argv) {
    if (argc < 3) {
        printf("not enough parameters. Usage: tpcd.exe mfile lfile\n");
    }
    pcdwrapper pw;

    printf("file name: %s\n", argv[1]);

    pw.readMFile(argv[1]);
    printf("%d vertices read.\n", pw.m_points.size());

    std::vector<unsigned int> I, J;
    std::vector<double> S;
    cal_pcd_laplace(pw.m_points, I, J, S);
    printf("pcd done for %s\n", argv[1]);
    
    save_IJS(argv[2], I, J, S);
    
/*
    long int n=pw.m_points.size();

    double* array, *lambda;

    array = new double[n*n];
    lambda = new double[n];

    array = textendpcd(I, J, S, n);
    lambda = new double[n];

    solve_eigen(array, lambda, n);

    writeDoubleRowSqMatrix(array, n, argv[2]);
    writeDoubleRow(lambda, n, argv[3]);

    delete[] array;
    delete[] lambda;
*/
    return (EXIT_SUCCESS);
}

